<template>
	<mescroll-uni ref="mescrollRef" @init="mescrollInit" height="100%" top="60" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback">
		<good-list :list="goods"></good-list>
	</mescroll-uni>
</template>

<script>
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
import MescrollMoreItemMixin from '@/components/mescroll-uni/mixins/mescroll-more-item.js';
import goodList from '@/pages/index/components/good-list.vue';
var _self;
export default {
	mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	components: {
		goodList
	},
	data() {
		return {
			downOption: {
				auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption: {
				auto: false, // 不自动加载
				noMoreSize: 4 //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
			},
			goods: [] //列表数据
		};
	},
	props: {
		tabs: Array, // tab菜单
		i: {
			//当前索引
			type: Number,
			default: 0
		},
		index: {
			//目前选择值
			type: Number,
			default: 0
		}
	},
	created(){
		_self=this;
	},
	methods: {
		/*下拉刷新的回调 */
		downCallback() {
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			_self.mescroll.resetUpScroll();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			let code = _self.tabs[_self.index].code;
			//console.log('code='+code);
			_self.getListDataFromNet(page.num, page.size, code).then(curPageData => {
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					_self.mescroll.endSuccess(curPageData.length);
					//设置列表数据
					if (page.num == 1) _self.goods = []; //如果是第一页需手动制空列表
					_self.goods = _self.goods.concat(curPageData); //追加新数据
					//console.log(_self.goods);
				})
				.catch(() => {
					//联网失败, 结束加载
					_self.mescroll.endErr();
				});
		},
		getListDataFromNet(pageNum, pageSize, code, successCallback, errorCallback) {
			try {
				const param = {
					catalogId: code
				};
				//console.log('getListDataFromNet',param)
				let promise = new Promise(function(resolve, reject) {
					_self.$http.httpRequest('taobao/productRecommend/catalogProductList', param, false).then(res => {
						//console.log('taobao/productRecommend/catalogProductList',res)
						if (res._tranStatus) {
							resolve(res._data.list);
						} else {
							let error = {};
							reject('error');
						}
					});
				});
				return promise;
			} catch (e) {
				return Promise.reject(e);
			}
		}
	}
};
</script>
